define(['app'], function( app ) {
    'use strict';

        app.directive('listTree', ['$compile', function ( $compile, $rootScope) {
            return {
                restrict: 'A',
                link: function ( scope, element, attrs ) {
                    var treeNodes = attrs.treeNodes;
                    var nodeId = attrs.nodeId || 'id';
                    var nodeLabel = attrs.nodeLabel || 'name';
                    var nodeChildren = attrs.nodeChildren || '_child';
                    var editFn = attrs.editFn || '';
                    scope.collapse = function ( _node, _obj ){
                        var ch = eval( _node[nodeChildren]);
                        if( angular.isArray( ch ) ){
                            var eleID = '#tree-cnt-' + eval( _node[nodeId]);
                            if( $(eleID).children(".list").is(":visible") ){
                                $(eleID).siblings("i").removeClass("minus").addClass("plus");
                                $(eleID).children(".list").hide();
                            }
                            else{
                                $(eleID).siblings("i").removeClass("plus").addClass("minus");
                                $(eleID).children(".list").show();
                            }
                        }
                    }

                    var tpl = ' <div class="item"  data-ng-repeat="node in '+treeNodes+'">' +
                            '<i class="blue minus icon" ng-if="node.'+nodeChildren+'"  style="cursor:pointer;" data-ng-click="collapse(node, this)" ></i>' +
                            '<i class="blue minus icon" ng-if="!node.'+nodeChildren+'"   ></i>' +
                            '<div class="content" id="tree-cnt-{{node.'+nodeId+'}}" >'+
                            '<div class="header"  ng-if="editFn != \'\' "   data-ng-click="nodeEdit(node)" >{{node.'+nodeLabel+'}}</div>'+
                            '<div class="header"  ng-if="editFn == \'\'" >{{node.'+nodeLabel+'}}</div>'+
                            '<div ng-if="node.'+nodeChildren+'" class="list" list-tree data-tree-nodes="node.'+nodeChildren+'" data-node-label="'+nodeLabel+
                            '"  data-node-children="'+nodeChildren+'" edit-fn="'+editFn+'"  data-node-id="'+nodeId+'" '+
                            '></div>'
                            '</div>' +
                            '</div>';
                    element.html('').append( $compile( tpl )( scope ) );
                }
            };
     }]);
});